library(tidyverse)
library(cowplot)
library(pdftools)
load(here::here('data/data.RData'))

data %>%
  mutate(
    SO = case_when(
      SO == "Austral: Brazilian Journal of Strategy and International Relations" ~ "Austral",
      SO == "Brazilian Journal of International Relations" ~ "BJIR",
      SO == "Conjuntura Austral: Journal of the Global South" ~ "Conjuntura Austral",
      SO == "Estudos Internacionais: Revista de Relações Internacionais da PUC Minas" ~"Estudos Internacionais",
      SO == "Meridiano 47 - Journal of Global Studies" ~ "Meridiano 47",
      SO == "Monções: Revista de Relações Internacionais da UFGD" ~ "Monções",
      SO == "Oikos - Revista de Economia Politica Internacional" ~ "Oikos",
      SO == "Revista Brasileira de Estudos de Defesa" ~ "RBED",
      SO == "Revista Brasileira de Politica Internacional" ~ "RBPI",
      TRUE ~ SO
      
    )
  ) %>% 
  filter(PY < 2022) -> data

data %>%
  separate_rows(OG, sep = ";") %>%
  distinct() %>%
  mutate(OG = str_trim(OG)) %>%
  filter(OG != "NA" & !is.na(OG)) %>%
  mutate(OG = if_else(OG == "Puc Minas","PUC/MG",OG),
         OG = if_else(OG == "PUC/Bras?lia","UCB",OG)) -> data_OG

data_OG %>%
  group_by(OG) %>%
  count() %>%
  arrange(desc(n)) %>%
  head(20) %>%
  ggplot(aes(x = n, y = reorder(OG,-n))) +
  geom_col(fill = "#444444") +
  geom_text(
    aes(label = n),
    hjust = 1.4,
    col = "white",
    fontface = "bold",
    size = 3
  ) +
  labs(
    x = "#",
    y = ""
  ) +
  theme_minimal() +
  scale_x_continuous(limits = c(0,650), breaks = c(0,650)) +
  theme(
    axis.text = element_text(color = '#444444'),
    plot.margin = margin(10,10,10,10),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    axis.title.x = element_text(
      margin = margin(t = 10)
    ),
    axis.title.y = element_text(
      margin = margin(r = 10)
    )
  ) -> plot8_1
plot8_1

ggsave(
  plot = plot8_1,
  filename = here::here('outputs/figure-04.pdf'),
  device = cairo_pdf,
  dpi = 400
)

pdf_convert(
  pdf = here::here('outputs/figure-04.pdf'),
  filenames = here::here('outputs/figure-04.jpeg'),
  format = "png", dpi = 400
)

